What is @babel/helpers?
The @babel/helpers package is part of the Babel toolchain, which is primarily used for converting ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. This specific package contains a set of functions that are used by Babel's transform plugins to avoid code duplication across generated output. These helpers are small snippets of code that perform common tasks used by the transformations, such as handling classes, spreading properties, etc.
What are @babel/helpers's main functionalities?
Class handling
This code demonstrates a helper function used by Babel to ensure that a class is only instantiated with the `new` keyword, preventing incorrect usage.
"use strict";\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\nvar MyClass = function MyClass() { _classCallCheck(this, MyClass); };
Spread properties
This helper function is used to emulate the behavior of the object spread operator `{...obj}`, allowing properties from one or more source objects to be copied into a new object.
"use strict";\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\nvar obj = _extends({}, sourceObj, { key: 'value' });
Other packages similar to @babel/helpers
core-js
Similar to @babel/helpers, core-js is a modular standard library for JavaScript, including polyfills for ECMAScript up to 2021. While @babel/helpers provides functions to support the transformation process, core-js focuses on polyfilling new JavaScript features for older environments.
regenerator-runtime
This package provides runtime support for generators and async functions, similar to how @babel/helpers supports various syntax transformations. It's often used in conjunction with Babel for projects that use generators or async/await syntax to ensure compatibility with older environments.
@babel/helpers
Collection of helper functions used by Babel transforms.
See our website @babel/helpers for more information.
Install
Using npm:
npm install --save-dev @babel/helpers
or using yarn:
yarn add @babel/helpers --dev